/*****************************************************************************
 * This source file is part of SBS (Screen Build System),                    *
 * which is a component of Screen Framework                                  *
 *                                                                           *
 * Copyright (c) 2008-2011 Ratouit Thomas                                    *
 *                                                                           *
 * This program is free software; you can redistribute it and/or modify it   *
 * under the terms of the GNU Lesser General Public License as published by  *
 * the Free Software Foundation; either version 3 of the License, or (at     *
 * your option) any later version.                                           *
 *                                                                           *
 * This program is distributed in the hope that it will be useful, but       *
 * WITHOUT ANY WARRANTY; without even the implied warranty of                *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser   *
 * General Public License for more details.                                  *
 *                                                                           *
 * You should have received a copy of the GNU Lesser General Public License  *
 * along with this program; if not, write to the Free Software Foundation,   *
 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA, or go to   *
 * http://www.gnu.org/copyleft/lesser.txt.                                   *
 *****************************************************************************/

package screen.tools.sbs.utils;

public class FieldPathType {
	public enum Type{
		ABSOLUTE,
		RELATIVE
	}
	
	Type position;
	boolean valid;
	
	public FieldPathType() {
		position = Type.RELATIVE;
		valid = true;
	}
	
	public void set(Type position){
		this.position = position;
		valid = true;
	}
	
	public Type get(){
		return position;
	}
	
	public void set(String pos){
		FieldString fs = new FieldString(pos);
		try {
			String pos2 = fs.getString();
			if("absolute".equals(pos2)){
				position = Type.ABSOLUTE;
				valid = true;
			}
			else if("relative".equals(pos2)){
				position = Type.RELATIVE;
				valid = true;
			}
			else{
				position = Type.RELATIVE;
				valid = false;
			}
		} catch (FieldException e) {
			position = Type.RELATIVE;
			valid = false;
		}
	}
	
	public FieldPath getFieldPath(String root, String relative){
		FieldPath fieldPath = null;
		if(position == Type.ABSOLUTE){
			fieldPath = new FieldPath(relative);
			fieldPath.setPathType(Type.ABSOLUTE);
		}
		else{
			fieldPath =  new FieldPath(root+"/"+relative);
		}
		return fieldPath;
	}
	
	public FieldFile getFieldFile(String root, String relative){
		FieldFile fieldFile = null;
		if(position == Type.ABSOLUTE){
			fieldFile = new FieldFile(relative);
			fieldFile.setPathType(Type.ABSOLUTE);
		}
		else{
			fieldFile =  new FieldFile(root+"/"+relative);
		}
		return fieldFile;
	}
}
